home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / icons / animator.zip / FRAME.C < prev    next >
C/C++ Source or Header  |  1993-04-16  |  12KB  |  380 lines

  1. #include "animator.h"
  2.  
  3. /* btw: WM_NCCREATE and WM_CREATE return BOOLs. 
  4.    Any message that needs *DefWndProc should call it
  5.    explicitly in its function. */
  6.  
  7. BOOL NEAR PASCAL Frame_OnCreate (HWND,LPCREATESTRUCT);
  8. VOID NEAR PASCAL Frame_OnInitMenu (HWND,HMENU);
  9. VOID NEAR PASCAL Frame_OnCommand (HWND,UINT,HWND,UINT);
  10. VOID NEAR PASCAL Frame_OnSize (HWND,UINT,short,short);
  11. LONG NEAR PASCAL Frame_OnDestroy (HWND);
  12. VOID NEAR PASCAL Frame_OnClose (HWND);
  13.  
  14.  
  15.  
  16.  
  17. //////////////////////////////////////////////////////////////////////////
  18. // FrameProc() - good example of how Message Crackers may be used.
  19. // Intercepts messages, handles the ones we hande, and DefWndProc()'s
  20. // the rest.
  21. //////////////////////////////////////////////////////////////////////////
  22.  
  23. LRESULT _export CALLBACK FrameProc (WNDPROC_PARAMS)
  24. {
  25.     switch (uMsg) 
  26.     {
  27.         HANDLE_MSG (hWnd, WM_CREATE,          Frame_OnCreate); 
  28.         HANDLE_MSG (hWnd, WM_INITMENU,        Frame_OnInitMenu);
  29.         HANDLE_MSG (hWnd, WM_COMMAND,         Frame_OnCommand);
  30.         HANDLE_MSG (hWnd, WM_SIZE,            Frame_OnSize);
  31.         HANDLE_MSG (hWnd, WM_CLOSE,           Frame_OnClose);
  32.         HANDLE_MSG (hWnd, WM_DESTROY,         Frame_OnDestroy);
  33.     }
  34.  
  35.     return (LRESULT)DefFrameProc(hWnd, _hwndClient, uMsg, wParam, lParam);
  36. }
  37.  
  38.  
  39.  
  40. //////////////////////////////////////////////////////////////////////////
  41. // BroadcastProc - This is simply the enumeration function for all of 
  42. // the child MDI windows.  It broadcasts a message to all alive MDI
  43. // children.  In our case, we just use it to broadcast the WM_CLOSE
  44. // message to everybody when the user wants to terminate session.
  45. //////////////////////////////////////////////////////////////////////////
  46.  
  47. BOOL _export CALLBACK BroadcastProc(HWND  hwnd,
  48.                   LONG  lParam)
  49. {
  50.     if (GetWindow(hwnd, GW_OWNER))
  51.     {
  52.         return TRUE;
  53.     }
  54.  
  55.     if (lParam == WM_CLOSE)
  56.     {
  57.         if (SendMessage(hwnd, WM_QUERYENDSESSION, 0, 0L))
  58.         {
  59.             MDI_Destroy (_hwndClient, hwnd);
  60.         }
  61.         else
  62.         {
  63.             return FALSE;
  64.         }
  65.     }
  66.     else
  67.     {
  68.         SendMessage(hwnd, (UINT)LOWORD(lParam), (WPARAM)0, 0L);
  69.     }
  70.  
  71.     return TRUE;
  72. }
  73.     
  74.  
  75.  
  76. //////////////////////////////////////////////////////////////////////////
  77. // Frame_OnCreate() - Restores position if there was anything in the
  78. // WIN.INI, then creates the client and status children.
  79. //////////////////////////////////////////////////////////////////////////
  80.  
  81. BOOL NEAR PASCAL Frame_OnCreate (HWND hwnd, CREATESTRUCT FAR* lpCS)
  82. {
  83.     CLIENTCREATESTRUCT  clicr;
  84.  
  85.     clicr.hWindowMenu = _hmenuChildWindow;
  86.     clicr.idFirstChild = IDM_FIRSTCHILD;
  87.  
  88.     _hwndClient = CreateWindow("MDICLIENT", NULL,
  89.                 WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
  90.                 0, 0, 0, 0, hwnd, ID_CLIENT, _hInst, (LPSTR)&clicr);
  91.  
  92.     if (!IsWindow(_hwndClient)) return FALSE;
  93.     
  94.     // we find out the y-extent of the status bar based on the
  95.     // height of the ansi-variable font.
  96.  
  97.     _nStatBarDY = GetANSITextHeight() + 7; // the '7' is the dead space
  98.  
  99.     _hwndStatus = CreateWindow (_szStatBarClass,NULL,
  100.                            WS_CHILD | WS_VISIBLE,
  101.                            0,0,0,0,
  102.                            hwnd,ID_STATBAR, _hInst, NULL);
  103.  
  104.     if (!IsWindow(_hwndStatus)) 
  105.     {
  106.         return FALSE;
  107.     }
  108.     
  109.     if (!SetTimer (hwnd, ID_TIMER, MINTIME, _lpfnTimer))
  110.     {
  111.         MESSAGE (IDS_TimerFail);
  112.         return FALSE;
  113.     }
  114.  
  115.     // Set up the Notify callback so we know when a task starts or dies...
  116.  
  117.     if (!NotifyRegister ((HTASK)GetWindowTask(hwnd), 
  118.         (LPFNNOTIFYCALLBACK)_lpfnNotify, NF_NORMAL))
  119.     {
  120.         MESSAGE (IDS_NotifyFail);
  121.         return FALSE;
  122.     }
  123.     
  124.     return TRUE;
  125. }
  126.  
  127.  
  128.  
  129. //////////////////////////////////////////////////////////////////////////
  130. // Frame_OnInitMenu() - Grays out menu items if needed, depending
  131. // on if something is currently animating, etc.
  132. //////////////////////////////////////////////////////////////////////////
  133.  
  134. VOID NEAR PASCAL Frame_OnInitMenu(HWND hwnd, HMENU hMenu)
  135. {   
  136.     HWND hwndChild = MDI_GetActive (_hwndClient);            
  137.  
  138.     if (IsWindow(hwndChild)) 
  139.     {
  140.         short    sChild = WINDOWNUM(hwndChild);
  141.         BOOL     b;
  142.         
  143.         b = (BOOL)(ISANIMATING(sChild) && SZEXELINK(sChild)[0] && TIMEINT(sChild));
  144.         EnableMenuItem (hMenu, IDM_STOP,        b ? MF_ENABLED : MF_GRAYED);
  145.         EnableMenuItem (hMenu, IDM_GO,          b ? MF_GRAYED : MF_ENABLED);
  146.         EnableMenuItem (hMenu, IDM_SETTINGS,    b ? MF_GRAYED : MF_ENABLED);
  147.         EnableMenuItem (hMenu, IDM_INSERTICON,  b ? MF_GRAYED : MF_ENABLED);
  148.         EnableMenuItem (hMenu, IDM_ADDICON,     b ? MF_GRAYED : MF_ENABLED);
  149.         EnableMenuItem (hMenu, IDM_DELETEICON,  b ? MF_GRAYED : MF_ENABLED);
  150.     }
  151.  
  152.     DefFrameProc(hwnd, _hwndClient, WM_INITMENU, (WPARAM)hMenu, 0L);
  153. }
  154.  
  155.  
  156.  
  157. //////////////////////////////////////////////////////////////////////////
  158. // Frame_OnCommand() - This is what handles file | new and file | open,
  159. // task exit/entry notification, and other things that logically need
  160. // to be handles in the Frame proc.
  161. //////////////////////////////////////////////////////////////////////////
  162.  
  163. VOID NEAR PASCAL Frame_OnCommand (HWND hwnd, UINT uMsg, HWND hChild, UINT uExtra) 
  164. {
  165.     short               sChild;
  166.     MDICREATESTRUCT     mdicr;
  167.     char                szBuff[32];
  168.     char                szFileName [MAX_FILE_SIZE];
  169.     HWND                hwndChild;
  170.  
  171.     switch (uMsg) 
  172.     {
  173.         case IDM_ABOUT:
  174.         {
  175.             DialogBox (_hInst, ABOUT, hwnd, _lpfnAbout);
  176.             break;
  177.         }
  178.         case IDM_NEW:
  179.         {
  180.             for (sChild = 0 ;
  181.                  _lPageFlags & (1 << sChild) ;
  182.                  ++sChild) ;
  183.  
  184.             if (sChild >= MAXANIMATIONS)
  185.             {
  186.                 MESSAGE (IDS_MaxAnimations);
  187.                 break;
  188.             }
  189.  
  190.             _lPageFlags |= 1 << sChild;
  191.  
  192.             wsprintf((LPSTR)szBuff,(LPSTR)"%s%d",
  193.                 (LPSTR)_szUntitled, sChild+1);
  194.  
  195.             mdicr.szClass   = (LPSTR)_szChildClass;
  196.             mdicr.szTitle   = (LPSTR)szBuff;
  197.             mdicr.hOwner    = _hInst;
  198.             mdicr.x         = CW_USEDEFAULT;
  199.             mdicr.y         = CW_USEDEFAULT;
  200.             mdicr.cx        = CW_USEDEFAULT;
  201.             mdicr.cy        = CW_USEDEFAULT;
  202.             mdicr.style     = 0;
  203.             mdicr.lParam    = (LPARAM)sChild;
  204.  
  205.             MDI_Create (_hwndClient, &mdicr);
  206.  
  207.             break;
  208.         }        
  209.  
  210.         case IDM_OPEN:
  211.             szFileName[0] = '\0';
  212.             OpenIconsInFile(szFileName) ;
  213.             // no break, because we need to refresh animations anyways...
  214.         case IDN_NEWTASK:   
  215.             RefreshAnimations ();
  216.             break;
  217.  
  218.         case IDN_EXITTASK:
  219.         {
  220.             short i;
  221.  
  222.             for (i=0 ; i<MAXANIMATIONS ; i++)
  223.             {
  224.                 if (IsWindow (HWNDANIM(i)) && !IsWindow(HWNDTARGET(i)))
  225.                 {
  226.                     SET_EXELOADED (i, FALSE);
  227.                     SET_HWNDTARGET (i, NULL);
  228.                     SET_HPREVICON (i, NULL);
  229.                 }
  230.             }
  231.  
  232.             break;
  233.         }
  234.  
  235.         case IDM_CLOSE:
  236.         {
  237.             if (hwndChild = MDI_GetActive (_hwndClient))
  238.             {
  239.                 FORWARD_WM_CLOSE (hwndChild, SendMessage); 
  240.             }
  241.             break;
  242.         }
  243.         case IDM_CLOSEALL:
  244.         {
  245.             if (_lPageFlags)
  246.             {
  247.                 EnumChildWindows(_hwndClient,(FARPROC)_lpfnBroadcast, 
  248.                     (LONG)WM_CLOSE);
  249.             }
  250.             break;
  251.         }
  252.         case IDM_EXIT:
  253.         {
  254.             SendMessage (hwnd, WM_CLOSE, 0, 0L);
  255.             break;
  256.         }
  257.         case WM_MDITILE:
  258.         case WM_MDICASCADE:
  259.         case WM_MDIICONARRANGE:
  260.         {
  261.             SendMessage(_hwndClient, uMsg, 0, 0L);
  262.             break;
  263.         }
  264.         default:
  265.         {
  266.         
  267.             if (IsWindow (hwndChild = MDI_GetActive (_hwndClient)))
  268.             {
  269.                 SendMessage(hwndChild, WM_COMMAND,
  270.                     uMsg, MAKELPARAM(hChild,uExtra));
  271.             }
  272.             
  273.             DefFrameProc(hwnd, _hwndClient, WM_COMMAND, 
  274.                 (WPARAM) uMsg, MAKELPARAM (hChild, uExtra)); 
  275.         
  276.             break;
  277.         }
  278.     }
  279. }
  280.  
  281.         
  282.  
  283. //////////////////////////////////////////////////////////////////////////
  284. // Frame_OnSize() - Changes the position of the client and status
  285. // children when the frame changes.
  286. //////////////////////////////////////////////////////////////////////////
  287.  
  288. VOID NEAR PASCAL Frame_OnSize (HWND hwnd, UINT wSizeType, short cx, short cy)            
  289. {
  290.     MoveWindow (_hwndClient, 0, 0, cx, cy-_nStatBarDY, TRUE);
  291.     MoveWindow (_hwndStatus, 0, cy-_nStatBarDY, cx, _nStatBarDY, TRUE);
  292. }
  293.  
  294.  
  295.  
  296. //////////////////////////////////////////////////////////////////////////
  297. // Frame_OnClose() - Does the queryendsession enumeration to the
  298. // child windows. If anyone does not agree, close doesn't really
  299. // close.
  300. //////////////////////////////////////////////////////////////////////////
  301.  
  302. VOID NEAR PASCAL Frame_OnClose (HWND hwnd)
  303. {
  304.     if (_lPageFlags)
  305.     {
  306.         if (!EnumChildWindows(_hwndClient,(FARPROC)_lpfnBroadcast, 
  307.             (LONG)WM_CLOSE))
  308.         {
  309.             return;
  310.         }
  311.     }
  312.  
  313.     DestroyWindow (hwnd);
  314. }
  315.  
  316.  
  317.  
  318. //////////////////////////////////////////////////////////////////////////
  319. // Frame_OnDestroy() - Handles the shutdown of the window.  Here,
  320. // the position of the window is recorded to the WIN.INI, the timer
  321. // is killed, the Register callback is unregistered, and the menus
  322. // are dealt with.
  323. //////////////////////////////////////////////////////////////////////////
  324.  
  325. LONG NEAR PASCAL Frame_OnDestroy (HWND hwnd)
  326. {
  327.     RecordPosition (hwnd);      
  328.     KillTimer (hwnd,ID_TIMER);
  329.     NotifyUnRegister (NULL);
  330.     SetMenu(hwnd,NULL);                  
  331.     DestroyMenu(_hmenuMain);
  332.     DestroyMenu(_hmenuChild);
  333.     PostQuitMessage(0);
  334.     return 0L;
  335. }
  336.  
  337.  
  338.  
  339.  
  340. //////////////////////////////////////////////////////////////////////////
  341. // RefreshAnimations() - This is what is called in response to a change
  342. // in the task list.  When toolhelp.dll notifies us of an NFW_STARTTASK
  343. // and when a script is opened, this updates our list of what is 
  344. // actually loaded in the respects of the list.
  345. //////////////////////////////////////////////////////////////////////////
  346.  
  347. VOID WINAPI RefreshAnimations (VOID)
  348. {
  349.     char    szModFileName[MAX_FILE_SIZE];
  350.     HWND    hwndNext;
  351.     short   i;
  352.  
  353.     for (i=0 ; i<MAXANIMATIONS ; i++)
  354.     {
  355.         if (!IsWindow(HWNDANIM(i))) continue;
  356.         if (IsWindow(HWNDTARGET(i))) continue;
  357.  
  358.         for (hwndNext = GetWindow (_hwndFrame,GW_HWNDFIRST) ;
  359.              hwndNext ; 
  360.              hwndNext = GetWindow (hwndNext,GW_HWNDNEXT))
  361.         {
  362.             GetModuleFileName (GetWindowInstance(hwndNext), 
  363.                 (LPSTR)szModFileName,sizeof (szModFileName));
  364.  
  365.             if (lstrcmp((LPSTR)szModFileName,(LPSTR)SZEXELINK(i)) == 0)
  366.             {
  367.                 SET_EXELOADED (i, TRUE);
  368.                 SET_HWNDTARGET (i, hwndNext);
  369.                 SET_HPREVICON (i, GetClassWord(hwndNext,GCW_HICON));
  370.                 if (AUTOANIMATE(i))
  371.                 {
  372.                         SET_ISANIMATING (i, TRUE);
  373.                 }
  374.             }
  375.         }
  376.     }
  377. }
  378.  
  379.  
  380.